import { Component } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  Events,
  ToastController
} from "ionic-angular";
import { PcSeriveProvider } from "../../../providers/pc-serive/pc-serive";
import { BaseUI } from "../../../common/baseUI";

/**
 * Generated class for the PcCreaterEvaluatingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-pc-creater-evaluating",
  templateUrl: "pc-creater-evaluating.html"
})
export class PcCreaterEvaluatingPage extends BaseUI {
  evalName: string = ""; //评测命名
  evalNames: string = ""; //评测命名
  startTime: string; //评测开始时间
  stopTime: string; //评测停止时间
  today; //获取当前日期的值
  ruleNnmber = { alldiff: 0, allsort: 0 }; //总分规则的大小
  teamcode; //团队code
  items; //显示小项目列表
  maxNum;//判断有没有重复单项的名称
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private pcSerive: PcSeriveProvider,
    private even: Events,
    private toastCtrl: ToastController
  ) {
    super();
    this.teamcode = this.navParams.get("teamcode");
    this.today = new Date();
    this.pcSerive.items = [];
  }
  ionViewDidEnter() {
    this.items = this.pcSerive.items;
  }

  //判断是不是emoji表情，我就不给你输入emoji表情
  isEmoji($event) {
    this.evalName = this.evalName.replace(/[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig, '');
    //限制输入长度
    if (this.evalName.length > 12) this.evalName = this.evalName.slice(0, 12);
  }

  //跳转小项目的页面
  newItem(id?) {
    if (!isNaN(id)) {
      this.navCtrl.push("PcRegistrationEvaluationPage", {
        callback: this.pcSerive.items[id],
        id: id,
      });
    } else {
      this.navCtrl.push("PcRegistrationEvaluationPage");
    }
  }

  //跳转总分规则页面 
  openTotal() {
    this.navCtrl.push("PcTotalRulePage", {
      numbers: this.pcSerive.items,
      ruleNnmber: this.ruleNnmber
    });
    this.even.subscribe("ruleNumber", numBer => {
      this.ruleNnmber = numBer;
    });
  }

  //提交按钮
  Submission() {
    this.repItemName();
    let startTime = new Date(this.startTime);
    let stopTime = new Date(this.stopTime);

    let maxName: boolean = true;
    for (const iterator of this.maxNum) {
      if (iterator['numBer'] > 1) maxName = false;
    }

    if (this.evalName == "") {
      super.showToast(this.toastCtrl, "请输入评测名称");
    } else if (this.evalName.length > 12) {
      super.showToast(this.toastCtrl, "评测名称过长，最大12个字");
    } else if (this.startTime == undefined) {
      super.showToast(this.toastCtrl, "请选择开始时间");
    } else if (this.stopTime == undefined) {
      super.showToast(this.toastCtrl, "请选择结束时间");
    } else if (startTime.valueOf() > stopTime.valueOf()) {
      super.showToast(this.toastCtrl, "时间选择错误");
    } else if (this.pcSerive.items.length == 0) {
      super.showToast(this.toastCtrl, "评测小项未填写");
    } else if (!maxName) {
      super.showToast(this.toastCtrl, "评测小项名称不能重复");
    } else if (this.ruleNnmber.alldiff == 0 || this.ruleNnmber.allsort == 0) {
      super.showToast(this.toastCtrl, "评测总分规则未填写");
    } else {
      let data = JSON.stringify(this.pcSerive.items);
      let ruledata = JSON.stringify(this.ruleNnmber).replace(/"/g, "'");
      let evalData = {
        evaluatingname: this.evalName,
        evaluatingtime: this.startTime,
        evaluatingendtime: this.stopTime,
        evaluatingproject: data,
        teamcode: this.teamcode,
        evaluatingrule: ruledata
      };
      this.pcSerive.createPc(evalData).subscribe(data => {
        if (!data) {
          this.showToast(this.toastCtrl, "创建失败");
        } else {
          this.showToast(this.toastCtrl, "创建成功");
          this.navCtrl.pop();
        }
      });
    }
  }

  //判断有没有重复的单项的名称
  repItemName() {
    this.maxNum = [];
    for (let i = 0; i < this.items.length; i++) {
      let a = this.items[i]["name"]; //值
      let b = 0; //出现次数
      for (let n = 0; n < this.items.length; n++) {
        if (this.items[n]["name"] == a) {
          b++;
        }
      }
      this.maxNum.push({ name: a, numBer: b });
    }
  }
}
